/**
 * 版权：山西书海传媒科技有限责任公司
 * 作者：吕俊雄
 * 更新时间：2019年3月29号
 */
Page({
	data: {
		current: 'A',
    to: 0,
    avatars:[],
	},
  onReady() {
    this.claculateTop();
  },
	claculateTop() {
    //初始化数据
    var that = this
		wx.showLoading({
			title: '数据正在加载中',
		})
    wx.request({
      url: 'https://wxapp.shuhai.cc/hh/selectTXL.action',
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        party_id: wx.getStorageSync("shuhai_party_id")
        //parayId: '374b8c05697be12201698041584d0005',
      },
      success: function (res) {
        if (res.data.status == "success") {
					//隐藏加载中
					wx.hideLoading();
          var aa = res.data.data;
          that.setData({
            avatars: aa
          })
        } else if (res.data.status == "fail") {
          wx.hideLoading();
          wx.showToast({
            title: '网络加载慢，请重新进入',      
            icon: 'none',
            duration: 2000
          })
          return
        }
        that.data.avatars.forEach((item) => {
          if (!item.enabled) return;
          (function (_item) {
            const query = wx.createSelectorQuery();
            const id = _item.id === '#' ? 'all' : item.id;
            query.select('#' + id).boundingClientRect((res) => {
              if (res) _item.top = res.top - 70;
              that.data.avatars = that.data.avatars;
            }).exec();
          })(item);
        });
      },
      fail: function (res) {
        wx.hideLoading();
        wx.showToast({
          title: '网络错误！',
          icon: 'none',
          duration: 2000
        })
        return;
      }
    });
	},
  goNameId:function(e){
    console.log(e);
    var id = e.currentTarget.dataset.id;
    var name = e.currentTarget.dataset.name;
    console.log(id + "====kkk===" + name);
    wx.navigateTo({
      url: '../tongxunId/tongxunId?id_s=' + id + "&name_s=" + name,
    })
  },
  switchLetter(e) {
    const { avatars } = this;
    const id = e.currentTarget.dataset.id;
    let avatar = this.data.avatars.find((o) => o.id === id);
    if (!avatar || !avatar.top === undefined) return;
    let tmp = {
      current: id
    };
    if (avatar.top !== undefined) {
      tmp.to = avatar.top;
    }
    this.setData(tmp);
  },
  whenScroll(e) {
    let cur = 'A';
    const { avatars, data } = this;
    const { current } = data;
    let len = this.data.avatars.length;
    while (len >= 0) {
      len--;
      let avatar = this.data.avatars[len];
      if (avatar.top !== undefined && avatar.top <= e.detail.scrollTop) {
        cur = avatar.id;
        break;
      }
    }
    if (current === cur) return;
    this.setData({
      current: cur
    });
  },
  searchCon: function (e) {
    var that = this;
    that.data.username = e.detail.value;
  },
  seatchStart: function (e) {
    //跳转搜索结果页
    var that = this;
		//展示加载中
		wx.showLoading({
			title: '加载中',
		})
    wx.request({
      url: 'https://wxapp.shuhai.cc/hh/selectTXL_ss.action',
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        username: that.data.username,
        party_id: wx.getStorageSync("shuhai_party_id")
      },
      success: function (res) {
        console.log(res.data.data);
        if (res.data.status == "success") {
					//隐藏加载中
					wx.hideLoading();
          var aa = res.data.data;
          that.setData({
            avatars: aa
          })
        } else if (res.data.status == "fail") {
          wx.hideLoading();
          wx.showToast({
            title: '无数据',
            icon: 'none',
            duration: 2000
          })
          return
        }
        //异步调用
        that.data.avatars.forEach((item) => {
          if (!item.enabled) return;
          (function (_item) {
            const query = wx.createSelectorQuery();
            const id = _item.id === '#' ? 'all' : item.id;
            query.select('#' + id).boundingClientRect((res) => {
              if (res) _item.top = res.top - 70;
              that.data.avatars = that.data.avatars;
            }).exec();
          })(item);
        });
      },
      fail: function (res) {
        wx.hideLoading();
        wx.showToast({
          title: '网络错误！',
          icon: 'none',
          duration: 2000
        })
        return;
      }
    })
  },
});